<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="角色名称" prop="roleName">
        <el-input
          v-model="queryParams.roleName"
          placeholder="请输入角色名称"
          clearable
          style="width: 240px"
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="default" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="default" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table
      v-show="checkType === 'multiple'"
      ref="dataTable"
      v-loading="loading"
      :data="roleList"
      @selection-change="handleMultipleRoleSelect"
    >
      <el-table-column type="selection" width="50" align="center" />
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>
    <el-table
      v-show="checkType === 'single'"
      v-loading="loading"
      :data="roleList"
      @current-change="handleSingleRoleSelect"
    >
      <el-table-column width="55" align="center">
        <template #default="scope">
          <el-radio v-model="radioSelected" :label="scope.row.roleId">{{ '' }}</el-radio>
        </template>
      </el-table-column>
      <el-table-column label="角色编号" prop="roleId" width="120" />
      <el-table-column label="角色名称" prop="roleName" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="权限字符" prop="roleKey" :show-overflow-tooltip="true" width="150" />
      <el-table-column label="显示顺序" prop="roleSort" width="100" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page-sizes="[5, 10]"
      layout="prev, pager, next"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />
  </div>
</template>

<script setup>
import { ref, watch, onMounted } from 'vue'
import { listRole } from '@/api/system/role'
import { StrUtil } from '@/utils/StrUtil'

// 定义props
const props = defineProps({
  selectValues: {
    type: [Number, String, Array],
    default: null,
    required: false
  },
  checkType: {
    type: String,
    default: 'multiple',
    required: false
  }
})

// 定义emits
const emits = defineEmits(['handleRoleSelect'])

// 定义响应式变量
const loading = ref(true)
const single = ref(true)
const multiple = ref(true)
const showSearch = ref(true)
const total = ref(0)
const roleList = ref([])
const open = ref(false)
const queryParams = ref({
  pageNum: 1,
  pageSize: 5,
  roleName: undefined,
  roleKey: undefined,
  status: undefined
})
const form = ref({})
const radioSelected = ref(0)
const selectRoleList = ref([])
const dataTable = ref(null)

// 监听selectValues变化
watch(() => props.selectValues, (newVal) => {
  if (StrUtil.isNotBlank(newVal)) {
    if (typeof newVal === 'number' || typeof newVal === 'string') {
      radioSelected.value = newVal
    } else {
      selectRoleList.value = newVal
    }
  }
}, { immediate: true })

// 监听roleList变化
watch(roleList, (newVal) => {
  if (newVal && selectRoleList.value.length > 0) {
    const nextTick = async () => {
      if (dataTable.value) {
        dataTable.value.clearSelection()
        selectRoleList.value.split(',').forEach(key => {
          const role = newVal.find(item => key == item.roleId)
          if (role) {
            dataTable.value.toggleRowSelection(role, true)
          }
        })
      }
    }
    nextTick()
  }
})

// 生命周期钩子
onMounted(() => {
  getList()
})

// 获取角色列表
const getList = () => {
  loading.value = true
  listRole(queryParams.value).then(response => {
    roleList.value = response.rows
    total.value = response.total
    loading.value = false
  })
}

// 多选框选中数据
const handleMultipleRoleSelect = (selection) => {
  const idList = selection.map(item => item.roleId)
  const nameList = selection.map(item => item.roleName)
  emits('handleRoleSelect', idList.join(','), nameList.join(','))
}

// 单选框选中数据
const handleSingleRoleSelect = (selection) => {
  if (selection) {
    radioSelected.value = selection.roleId
    const roleName = selection.roleName
    emits('handleRoleSelect', radioSelected.value.toString(), roleName)
  }
}

// 搜索框按钮操作
const handleQuery = () => {
  queryParams.value.pageNum = 1
  getList()
}

// 重置按钮操作
const resetQuery = () => {
  queryParams.value = {
    pageNum: 1,
    pageSize: 5,
    roleName: undefined,
    roleKey: undefined,
    status: undefined
  }
  handleQuery()
}
</script>

<style>
/* 隐藏radio展示的label及本身自带的样式 */
.el-radio__label {
  display: none;
}
</style>